<?php include('../includes/header.php'); ?>
<?php include('../includes/mysqli_connect.php'); ?>
<?php include('../includes/function.php'); ?>
<?php include('../includes/slide-admin.php'); ?>
  
    <?php 
        if(isset($_GET['cid']) && filter_var($_GET['cid'],FILTER_VALIDATE_INT,array('min_range'=>1)))
        {
            $cid=$_GET['cid'];
        }else{
        redirect_to('admin/admin.php');
        }
        
    
        if($_SERVER['REQUEST_METHOD'] == 'POST')
        {
            //GIA TRI TON TAI , XU LY FORM
            $errors = array();
            if(empty($_POST['category'])){
                 $errors[]="category";             
            } else {
            $cat_name=mysqli_real_escape_string($dbc,strip_tags($_POST['category']));
            }            
            if(isset($_POST['position']) && filter_var($_POST['position'],FILTER_VALIDATE_INT,array('min_range'=>1))){
            $position=$_POST['position'];
            }
            else{
                $errors[]="position";            
            }          
            if(empty($errors))
            {                  
            $q="UPDATE categories set cat_name='{$cat_name}' , position={$position} where cat_id={$cid}";
            $r = mysqli_query($dbc,$q) ;
            confirm_query($r,$q);
             if(mysqli_affected_rows($dbc)==1){
               $messages= "<p class='success'>The caregories was edited successfully.</p>";                                                
             }else{
               $messages= "<p class='warning'>Could not edit to the database due to a system error .</p>";
             }                                   
            } else {
               $messages= "<p class='warning'>please fill all the required fields.</p> ";
            }                                                                              
        }    
    ?>
     <div id="content">
     <?php 
     $q="SELECT cat_name , position FROM categories WHERE cat_id={$cid}";
     $r=mysqli_query($dbc,$q);
     confirm_query($r,$q);
     if(mysqli_num_rows($r) ==1){
        //Neu category ton tai trong db , dua vao cid . xuat du lieu ra ngoai trinh duyet 
        list($cat_name,$position)= mysqli_fetch_array($r,MYSQLI_NUM);        
     } else {
        $messages = "<p class='warning'> The category does not exist</p>";
     }
     ?>
     <h2>Edit a Category 
        <?php if(isset($cat_name)) echo $cat_name;
        ?>
     </h2>
     <?php
        if(!empty($messages))echo $messages;
     ?>
      <form id="edit_cat" action="" method="post">
            <fieldset>
                <legend>Edit category</legend>
                <div>
                    <label for="category">Category name : <span class="required">*</span></label>
                    <?php if(isset($errors) && in_array('category',$errors))
                            {echo "<p class='warning'>Please fill the category name </p>";}
                    ?>
                    
                    <input type="text" name="category" id="category" value="<?php if(isset($cat_name)) echo $cat_name;
        ?>" size="20" maxlength="150" tabindex="1" />                                    
                </div>
                <div>
                <label for="position"> Position : <span class="required">*</span></label>
                     <?php if(isset($errors) && in_array('position',$errors))
                            {echo "<p class='warning'>Please select the position  </p>";}
                    ?>
                <select name="position" tabindex="2">
                  <?php
                    $q="SELECT count(cat_id) AS count from categories";
                    $r=mysqli_query($dbc,$q) or die ("Query {$q} <br /> MySql Error  ".mysqli_error($dbc));;
                    if(mysqli_num_rows($r)==1) {
                        list($num)=mysqli_fetch_array($r,MYSQLI_NUM);
                        for ($i=1;$i <=$num+1;$i++)//tao vong for de ra option +1  gia tri cho position
                        {
                            echo "<option value='{$i}'";
                            
                                if(isset($position) && $position==$i) echo "selected='selected'";
                            
                            echo ">".$i."</option>";
                        }   
                    }
                  ?>
                 
                </select>
                </div>
            
            </fieldset>
      
        <input type="submit" value="Add Category"/>
      </form>

    </div><!--end content-->
<?php include('../includes/slide-b.php'); ?>
<?php include('../includes/footer.php'); ?>
    
    